package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.IDecorateDataService;
import club.haomoon.boss.vo.article.DecorateDataArticleVo;
import club.haomoon.core.entity.article.ArticleData;
import club.haomoon.core.entity.article.DecoratePageData;
import club.haomoon.core.mapper.article.ArticleDataMapper;
import club.haomoon.core.mapper.article.DecoratePageDataMapper;
import club.haomoon.core.util.DateUtil;
import club.haomoon.core.util.PropertyUtil;
import club.haomoon.core.util.StringUtils;
import club.haomoon.core.util.UrlUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * -------------------------
 * @Description: 装修数据服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/18 17:24
 * -------------------------
 */
@Service
public class DecorateDataServiceImpl implements IDecorateDataService {

    @Resource
    ArticleDataMapper articleDataMapper;

    @Resource
    DecoratePageDataMapper decoratePageDataMapper;

    /**
     * 获取文章数据
     * @param limit 条数
     * @return List<DecorateArticleDataVo>
     */
    @Override
    public List<DecorateDataArticleVo> article(Integer limit) {

        List<ArticleData> articles = articleDataMapper.selectList(new QueryWrapper<ArticleData>()
                .eq("is_show", 1)
                .orderByDesc("id")
                .last("limit " + limit));
        List<DecorateDataArticleVo> list = new LinkedList<>();
        for (ArticleData article : articles) {
            DecorateDataArticleVo vo = new DecorateDataArticleVo();
            BeanUtils.copyProperties(article, vo);
            vo.setImage(UrlUtil.toAdminAbsoluteUrl(vo.getImage()));
            Integer clickActual = vo.getClickActual() != null ? vo.getClickActual() : 0;
            Integer clickVirtual = vo.getClickVirtual() != null ? vo.getClickVirtual() : 0;
            Integer click = clickActual + clickVirtual;
            vo.setClick(click);
            vo.setCreateTime(DateUtil.timestampToDate(Long.valueOf(vo.getCreateTime())));
            list.add(vo);
        }
        return list;
    }

    @Override
    public JSONObject pc() {

        JSONObject pc = new JSONObject();
        DecoratePageData decoratePage = decoratePageDataMapper.selectById(4);
        Assert.notNull(decoratePage, "数据不存在!");
        String frontUrl = PropertyUtil.get("cms.front-url");
        frontUrl = StringUtils.isNotEmpty(frontUrl) && frontUrl.endsWith("/") ? frontUrl + "pc" : frontUrl + "/pc";

        pc.put("update_time", DateUtil.timestampToDate(decoratePage.getUpdateTime()));
        pc.put("pc_url",  frontUrl);
        return pc;
    }
}
